package cc.vicp.djx314.starfood.web.personnel.order;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cc.vicp.djx314.starfood.domain.account.Personnel;
import cc.vicp.djx314.starfood.service.order.OrderService;
import cc.vicp.djx314.starfood.web.exception.UserSessionTimeOutException;

@Controller
public class OrderFolwEnsureController {
	public static final Logger logger = LoggerFactory.getLogger(OrderFlowDrawController.class);

	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/personnel/flow/order/ensure/{orderId}/{status}",
			method = RequestMethod.PUT)
	public ModelAndView orderFolwEnsure(@PathVariable("orderId") Integer orderId,
			@PathVariable("status") Integer status, HttpSession session)
					throws UserSessionTimeOutException {
		Personnel loginPersonnel = (Personnel) session.getAttribute("loginPersonnel");
		if (loginPersonnel == null) {
			throw new UserSessionTimeOutException("管理员session过期");
		}
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		boolean isSuccess = false;
		try {
			if (OrderService.FINAL_STATUS.equals(status)) {
				orderService.updateOrderFlowStatusFinish(orderId, status, loginPersonnel);
			} else {
				orderService.updateOrderFlowStatus(orderId, status);
			}
			isSuccess = true;
		} catch (Exception e) {
			logger.info("更新订单状态出错", e);
		}
		model.addObject("success", isSuccess);
		return model;
	}
}